package com.lzb.browser.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.browser.R;
import com.lzb.browser.basic.dialog.BaseSmartDialog;
import com.lzb.browser.databinding.DialogAddAlbumBinding;
import com.lzb.browser.databinding.DialogAddCollectBinding;

public class AddCollectDialog extends BaseSmartDialog<DialogAddCollectBinding> implements View.OnClickListener {

    private boolean isAdd;
    private String saveTitle, saveUrl;

    public AddCollectDialog(@NonNull Context context) {
        super(context);
    }

    public void setAdd(boolean add) {
        isAdd = add;
    }

    public void setSaveTitle(String saveTitle) {
        this.saveTitle = saveTitle;
    }

    public void setSaveUrl(String saveUrl) {
        this.saveUrl = saveUrl;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_add_collect;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        if (isAdd) {
            mDataBinding.tvTitle.setText(getContext().getString(R.string.add_collect));
        } else {
            mDataBinding.tvTitle.setText(getContext().getString(R.string.edit_collect));
        }
        mDataBinding.etTitle.setText(saveTitle);
        mDataBinding.etUrl.setText(saveUrl);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            String title = mDataBinding.etTitle.getText().toString().toString().trim();
            String url = mDataBinding.etUrl.getText().toString().toString().trim();
            if (TextUtils.isEmpty(title)) {
                ToastUtils.showShort(R.string.please_input_title);
                return;
            }
            if (TextUtils.isEmpty(url)) {
                ToastUtils.showShort(R.string.please_input_link);
                return;
            }
            dismiss();
            if (listener != null) {
                listener.clickConfirm(title, url);
            }
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(String title, String url);
    }
}
